package com.jy.controller;

import com.jy.domain.Student;
import com.jy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

//    查询所有的学生信息
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Student> list = studentService.findAll();
        modelAndView.setViewName("students");
        modelAndView.addObject("list",list);

        return modelAndView;
    }

//    删除一个学生
    @RequestMapping("/deleteStu")
    public ModelAndView deleteStu(int sid){
        ModelAndView modelAndView = new ModelAndView();
        int result = studentService.deleteStu(sid);
        modelAndView.setViewName("redirect:findAll");

        return modelAndView;
    }

//   查询学生个人信息
    @RequestMapping("/updateStu")
    public ModelAndView findById(int id){
        ModelAndView modelAndView = new ModelAndView();
        Student student = studentService.findById(id);
        modelAndView.addObject("student",student);
        modelAndView.setViewName("students_edit");

        return modelAndView;
    }

//    修改学生个人信息
    @RequestMapping("/update")
    public ModelAndView update(Student student){
        ModelAndView modelAndView = new ModelAndView();
        if(studentService.updateStudent(student) > 0){
            modelAndView.setViewName("redirect:findAll");
        }else {
            modelAndView.setViewName("redirect:findAll");
        }

        return modelAndView;
    }

//    添加学生
    @RequestMapping("/saveStudent")
    public ModelAndView saveStudent(Student student){
        ModelAndView modelAndView = new ModelAndView();
        Student stu = studentService.findBySId(student.getSid());
        if(stu == null){//要添加的学号不存在
            studentService.saveStudent(student);
            modelAndView.setViewName("redirect:findAll");
        }else {
            modelAndView.setViewName("students_insert");
            String saveError = "学号已存在，检查学号是否正确";
            modelAndView.addObject("saveError",saveError);
        }

        return modelAndView;
    }

//    删除选中的学生
    @RequestMapping("/deleteSomeStudent")
    public String deleteSomeStu(int[] str){
//        for (int i: str){
//            System.out.println(i);
//        }
        if(str.length > 0){
            studentService.deleteStudents(str);
        }
        return "redirect:findAll";
    }

//    通过学生的名字查询对应学生
    @RequestMapping("/findByName")
    public ModelAndView findByNameLike(String str){
        ModelAndView modelAndView = new ModelAndView();
        if (str != ""){
            List<Student> someStu = studentService.findByNameLike(str);
            modelAndView.setViewName("someStudents");
            modelAndView.addObject("someStu",someStu);
        }else {
            modelAndView.setViewName("someStudents");
        }

        return modelAndView;
    }



    @RequestMapping("/{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }
}
